package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.dto.DishDto;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.mapper.DishMapper;
import com.reggie.service.DishFlavorService;
import com.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional  //开启事务
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品
        this.save(dishDto);

        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //使用流遍历 并赋值菜品id值
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表
        dishFlavorService.saveBatch(flavors);


    }

    /**
     * 根据id 查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishDto dishDto = new DishDto();

        //菜品信息
        Dish  dish= this.getById(id);

        BeanUtils.copyProperties(dish,dishDto);

        //口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品信息，同时跟新对应的口味信息
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> list = new ArrayList<>();
        for (DishFlavor dishFlavor: flavors) {
            dishFlavor.setDishId(dishDto.getId());
            list.add(dishFlavor);
        }

        dishFlavorService.saveBatch(list);

    }

    /**
     * 停售
     * @param ids
     */
    @Override
    public void stop(List<Long> ids) {
        List<Dish> list = new ArrayList<>();
        for (Long id:ids){
            Dish dish = this.getById(id);
            dish.setStatus(0);
            list.add(dish);
        }
        this.updateBatchById(list);
    }

    /**
     * 启售
     * @param ids
     */
    @Override
    public void start(List<Long> ids) {
        List<Dish> list = new ArrayList<>();
        for (Long id:ids){
            Dish dish = this.getById(id);
            dish.setStatus(1);
            list.add(dish);
        }
        this.updateBatchById(list);

    }
}
